package com.zhongxi.guoyuan.wxapi;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.text.Editable;
import android.text.Html;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.zhongxi.guoyuan.OffLineShop.ConnectNet.NetWork;
import com.zhongxi.guoyuan.OffLineShop.UI.SurePayActivity;
import com.zhongxi.guoyuan.OffLineShop.UrlOffLine.RelativeUrl;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.entity.BaseResponse;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.utils.CommonTool;
import com.zhongxi.guoyuan.utils.Constants;
import com.zhongxi.guoyuan.utils.DataUtil;
import com.zhongxi.guoyuan.utils.SpUtils;
import com.zhongxi.guoyuan.utils.ToastUtil;
import com.zhongxi.guoyuan.utils.UiUtils;
import com.zhongxi.guoyuan.view.ConsumerLoveYieldActivity;
import com.zhongxi.guoyuan.view.MainHomeActivity;
import com.zhongxi.guoyuan.view.PaySuccessActivity;
import com.zhongxi.guoyuan.view.personalCenter.ClearWeedDetailActivity;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import okhttp3.Call;


public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";

    private IWXAPI api;
    private TextView tvTitle;
    private ImageView ivBack;
    private DialogInterface.OnKeyListener keyListener = new DialogInterface.OnKeyListener() {
        @Override
        public boolean onKey(DialogInterface dialogInterface, int keyCode, KeyEvent keyEvent) {
            if (keyCode == KeyEvent.KEYCODE_BACK && keyEvent.getRepeatCount() == 0) {
                Intent intent = new Intent(WXPayEntryActivity.this, MainHomeActivity.class);
                startActivity(intent);
                finish();
                return true;
            }
            return false;
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_result);
        tvTitle = (TextView) findViewById(R.id.tvTitle);
        ivBack = (ImageView) findViewById(R.id.ivBack);
        tvTitle.setText("支付成功");

        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(WXPayEntryActivity.this, MainHomeActivity.class);
                startActivity(intent);
                finish();
            }
        });

        api = WXAPIFactory.createWXAPI(this, Constants.APP_ID);
        api.handleIntent(getIntent(), this);

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(BaseResp resp) {
        int code = resp.errCode;
        if (code == -2) {
            tvTitle.setText("支付失败");  //支付取消
            if (null != MyApplication.getInstance().getPay() && !"".equals(MyApplication.getInstance().getPay())) {
                if ("facetoface".equals(MyApplication.getInstance().getPay())) {
                    getFaceToFaceFailing();
                    showFaceWindow();
                } else if ("upface".equals(MyApplication.getInstance().getPay()) || "offline".equals(MyApplication.getInstance().getPay())||"exchange".equals(MyApplication.getInstance().getPay())) {
                    showFailFacePw();
                } else {
                    showFailPopWindow();
                }
            }
        } else if (code == -1) {   //支付失败
            tvTitle.setText("支付失败");
            if (null != MyApplication.getInstance().getPay() && !"".equals(MyApplication.getInstance().getPay())) {
                if ("facetoface".equals(MyApplication.getInstance().getPay())) {    //当面付
                    getFaceToFaceFailing();
                    showFaceWindow();
                } else if ("upface".equals(MyApplication.getInstance().getPay()) || "offline".equals(MyApplication.getInstance().getPay())||"exchange".equals(MyApplication.getInstance().getPay())) {
                    showFailFacePw();
                } else {
                    showFailPopWindow();
                }
            }
        } else {
            if (null != MyApplication.getInstance().getPay() && !"".equals(MyApplication.getInstance().getPay())) {
                if (MyApplication.getInstance().getPay().equals("beauty")) {
                    showPopupWindow();
                } else if ("facetoface".equals(MyApplication.getInstance().getPay())) {
                    MyApplication.getInstance().sharedPrefrences.edit().putString("orderId", "").commit();
                    MyApplication.getInstance().sharedPrefrences.edit().putString("certificate", "").commit();
                    showRewardWindow();  //可进红豆宝
                } else if ("offline".equals(MyApplication.getInstance().getPay())) {
                    showPaySuccessDialogOffLine();
                }else if ("exchange".equals(MyApplication.getInstance().getPay())){
                    showExchangeSuccessDialog();
                }else {   //if (MyApplication.getInstance().getPay().equals("reward"))
                    showRewardWindow();  //可进红豆宝
                }
            }
        }
    }

    /***
     * 豆豆兑换支付成功
     */
    private void showExchangeSuccessDialog() {
        LayoutInflater inflater = LayoutInflater.from(this);
        LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.dialog_pay_fail, null);
        final AlertDialog dialog = new AlertDialog.Builder(WXPayEntryActivity.this).create();
        dialog.setCanceledOnTouchOutside(false);
        dialog.setOnKeyListener(keyListener);
        dialog.show();
        dialog.getWindow().setContentView(layout);
        TextView tvContent = (TextView) layout.findViewById(R.id.tvContent);
        tvContent.setVisibility(View.GONE);
        ImageView iv_Cancel = (ImageView) layout.findViewById(R.id.iv_Cancel);
        iv_Cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
                onBackPressed();//返回豆豆兑换详情页去评价
                finish();
            }
        });
        TextView btSure = (TextView) layout.findViewById(R.id.btSure);
        btSure.setText("去评价");
        btSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                onBackPressed();//返回豆豆兑换详情页去评价
                finish();
            }
        });
    }

    /**
     * 线下超市商家
     */
    private void showPaySuccessDialogOffLine() {
        final android.app.AlertDialog mDialog = new android.app.AlertDialog.Builder(WXPayEntryActivity.this).create();
        View view = UiUtils.inflate(R.layout.dialog_pay_success_offline);
        ImageView mImageVie = (ImageView) view.findViewById(R.id.iv_cancel);
        TextView tv_incentive_meidou = (TextView) view.findViewById(R.id.tv_incentive_meidou);
        final RatingBar mRatingBar = (RatingBar) view.findViewById(R.id.ratingBar);
        final EditText et_evaluate = (EditText) view.findViewById(R.id.et_evaluate);
        final TextView long_evaluate = (TextView) view.findViewById(R.id.long_evaluate);
        Button btn_reset = (Button) view.findViewById(R.id.btn_reset);
        Button btn_submit = (Button) view.findViewById(R.id.btn_submit);
        mImageVie.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mDialog.dismiss();
                finish();
                startActivity(new Intent(UiUtils.getContext(), MainHomeActivity.class));
            }
        });
        tv_incentive_meidou.setText(SpUtils.getInstance().readSp("mdffline") + " 美豆");
        btn_reset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mRatingBar.setRating(0);
                et_evaluate.setText("");
            }
        });
        btn_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String mTrim = et_evaluate.getText().toString().trim();
                if (!TextUtils.isEmpty(mTrim) && mRatingBar.getRating() != 0) {
                    String rb = String.valueOf(mRatingBar.getRating());
                    addComment(mTrim, Integer.parseInt(rb.substring(0, rb.indexOf("."))));
                    mDialog.dismiss();
                } else {
                    ToastUtil.show(getString(R.string.comment_nofinish));
                }
            }
        });
        et_evaluate.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                long_evaluate.setText("0/200");
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                long_evaluate.setText(s.length() + "/200");
            }
        });
        mDialog.setCanceledOnTouchOutside(false);
        mDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialogInterface, int i, KeyEvent keyEvent) {
                if (i == KeyEvent.KEYCODE_BACK && keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
                    mDialog.dismiss();
                    finish();
                    startActivity(new Intent(UiUtils.getContext(), MainHomeActivity.class));
                    return true;
                }
                return false;
            }
        });
        mDialog.setView(view);
        mDialog.show();
    }

    private Disposable mDisposable;

    private void addComment(String trim, int rating) {
        unsubscribe();
        mDisposable = NetWork.getOffLineApi().addCommentApi(RelativeUrl.ADDCOMMENT + MyApplication.getInstance().getUrl(),
                MyApplication.getInstance().getCheck(false), SpUtils.getInstance().readSp("shopid"), SpUtils.getInstance().readSp("orderid"), rating, trim)
                .subscribeOn(Schedulers.io())
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                    }
                })
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<BaseResponse>() {
                    @Override
                    public void accept(BaseResponse baseResponse) throws Exception {
                        MyApplication.getInstance().setSpValue(baseResponse.getSafeobj());
                        if (baseResponse.getCode() != 1) {
                            ToastUtil.show(getString(R.string.losePay, baseResponse.getCode()));
                        }
                        finish();
                        startActivity(new Intent(UiUtils.getContext(), MainHomeActivity.class));
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        ToastUtil.show(getString(R.string.load_error));
                        finish();
                        startActivity(new Intent(UiUtils.getContext(), MainHomeActivity.class));
                    }
                });
    }


    private void showFailFacePw() {
        LayoutInflater inflater = LayoutInflater.from(this);
        LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.dialog_pay_fail, null);
        final AlertDialog dialog = new AlertDialog.Builder(this).create();
        dialog.setCanceledOnTouchOutside(false);
        Window window = dialog.getWindow();
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        dialog.show();
        dialog.getWindow().setContentView(layout);
        TextView tv_title = (TextView) layout.findViewById(R.id.tv_title);
        tv_title.setText("抱歉, 您支付未成功!");
        TextView tvContent = (TextView) layout.findViewById(R.id.tvContent);
        tvContent.setVisibility(View.GONE);
        ImageView iv_Cancel = (ImageView) layout.findViewById(R.id.iv_Cancel);
        dialog.setOnKeyListener(keyListener);
        iv_Cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
                finish();
                Intent intent = new Intent(WXPayEntryActivity.this, MainHomeActivity.class);
                startActivity(intent);
            }
        });
        TextView btSure = (TextView) layout.findViewById(R.id.btSure);
        btSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                finish();
                Intent intent = new Intent(WXPayEntryActivity.this, MainHomeActivity.class);
                startActivity(intent);
            }
        });
    }

    private void getFaceToFaceFailing() {
        Map<String, String> map = new HashMap<>();
        map.put("orderid", MyApplication.getInstance().getQrPayFailOrderId());
        map.put("certificate", MyApplication.getInstance().getQrPayFailCertificate());
        String baseUrl = "/" + MyApplication.getInstance().getUrl() + "?params=" + MyApplication.getInstance().getCheck(false);
        OkHttpUtils.post()
                .url(Url.QRCODEFAILE + baseUrl)
                .params(map)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        System.out.println("on error 扫码支付失败::" + e.getMessage().toString());
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        try {
                            JSONObject obj = new JSONObject(response);
                            int code = obj.getInt("code");
                            if (code == 1) {
                                processSuccessResult(response);
                            } else {
                                ProcessFailResult(response);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    private void ProcessFailResult(String response) {
        Gson gson = new Gson();
        BaseResponse baseResponse = gson.fromJson(response, BaseResponse.class);
        MyApplication.getInstance().setSpValue(baseResponse.getSafeobj());
    }

    private void processSuccessResult(String response) {
        Gson gson = new Gson();
        BaseResponse baseResponse = gson.fromJson(response, BaseResponse.class);
        MyApplication.getInstance().setSpValue(baseResponse.getSafeobj());
        if (baseResponse.getCode() == 1000) {
            Toast.makeText(this, "参数错误", Toast.LENGTH_SHORT).show();
        }
    }

    private void showFaceWindow() {
        LayoutInflater inflater = LayoutInflater.from(this);
        LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.dialog_pay_fail, null);
        final AlertDialog dialog = new AlertDialog.Builder(WXPayEntryActivity.this).create();
        dialog.setCanceledOnTouchOutside(false);
        dialog.setOnKeyListener(keyListener);
        dialog.show();
        dialog.getWindow().setContentView(layout);
        TextView tvContent = (TextView) layout.findViewById(R.id.tvContent);
        tvContent.setVisibility(View.GONE);
        ImageView iv_Cancel = (ImageView) layout.findViewById(R.id.iv_Cancel);
        iv_Cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
                Intent intent = new Intent(WXPayEntryActivity.this, MainHomeActivity.class);
                startActivity(intent);
                finish();
            }
        });
        TextView btSure = (TextView) layout.findViewById(R.id.btSure);
        btSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                Intent intent = new Intent(WXPayEntryActivity.this, MainHomeActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

    private void showShopWindow() {
        LayoutInflater inflater = LayoutInflater.from(this);
        LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.dialog_pay_success, null);
        final AlertDialog dialog = new AlertDialog.Builder(this).create();
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
        dialog.getWindow().setContentView(layout);
        dialog.setOnKeyListener(keyListener);
        ImageView iv_Cancel = (ImageView) layout.findViewById(R.id.iv_Cancel);
        iv_Cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
                Intent intent = new Intent(WXPayEntryActivity.this, MainHomeActivity.class);
                startActivity(intent);
                finish();
            }
        });

        TextView tv_dec = (TextView) layout.findViewById(R.id.tv_dec);
        tv_dec.setText("本次消费您获得:");
        TextView tvContent = (TextView) layout.findViewById(R.id.tvContent);
        tvContent.setVisibility(View.VISIBLE);
        String mSubstring = DataUtil.formatData(MyApplication.getInstance().getWinXinMD()).substring(0, DataUtil.formatData(MyApplication.getInstance().getWinXinMD()).length() - 3);
        tvContent.setText(mSubstring + " 美豆");
        TextView btSure = (TextView) layout.findViewById(R.id.btSure);
        btSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                Intent intent = new Intent(WXPayEntryActivity.this, MainHomeActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

    /**
     * 返银商品支付成功
     */
    private void showRewardWindow() {
        LayoutInflater inflater = LayoutInflater.from(this);
        LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.dialog_pay_story_success, null);
        final AlertDialog dialog = new AlertDialog.Builder(this).create();
        dialog.setCanceledOnTouchOutside(false);
        dialog.setOnKeyListener(keyListener);
        dialog.show();
        dialog.getWindow().setContentView(layout);
        ImageView iv_Cancel = (ImageView) layout.findViewById(R.id.iv_Cancel);
        iv_Cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
                Intent intent = new Intent(WXPayEntryActivity.this, MainHomeActivity.class);
                startActivity(intent);
                finish();
            }
        });
        TextView tv_md = (TextView) layout.findViewById(R.id.tv_md);
        TextView tv_hd = (TextView) layout.findViewById(R.id.tv_hd);
        if (DataUtil.moreThanZero(MyApplication.getInstance().getWinXinMD()) && DataUtil.moreThanZero(MyApplication.getInstance().getWinXinHD())) {
            tv_hd.setVisibility(View.VISIBLE);
            tv_md.setText(Html.fromHtml("<font color='#ff7f00'>" + CommonTool.getWhole(MyApplication.getInstance().getWinXinMD()) + "美豆+</font><font color='#ff4c4c'>"
                    + DataUtil.multiplys(MyApplication.getInstance().getWinXinMD(), "0.05", 2) + "红豆</font>"));
            tv_hd.setText(MyApplication.getInstance().getWinXinHD() + " 红豆(分期)");
        } else if (!DataUtil.moreThanZero(MyApplication.getInstance().getWinXinMD()) && DataUtil.moreThanZero(MyApplication.getInstance().getWinXinHD())) {
            tv_hd.setVisibility(View.GONE);
            tv_md.setText(MyApplication.getInstance().getWinXinHD() + " 红豆(分期)");
            tv_md.setTextColor(getResources().getColor(R.color.title_red));
        } else {
            tv_hd.setVisibility(View.GONE);
            tv_md.setText(Html.fromHtml("<font color='#ff7f00'>" + CommonTool.getWhole(MyApplication.getInstance().getWinXinMD()) + "美豆+</font><font color='#ff4c4c'>"
                    + DataUtil.multiplys(MyApplication.getInstance().getWinXinMD(), "0.05", 2) + "红豆</font>"));
        }
        TextView tv_open = (TextView) layout.findViewById(R.id.tv_open);
        TextView tv_know = (TextView) layout.findViewById(R.id.tv_know);
        tv_open.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
                finish();
                Intent intent = new Intent(WXPayEntryActivity.this, ConsumerLoveYieldActivity.class);
                startActivity(intent);
            }
        });

        tv_know.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                Intent intent = new Intent(WXPayEntryActivity.this, MainHomeActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

    /**
     * 美币美容院支付成功
     */
    private void showPopupWindow() {
        LayoutInflater inflater = LayoutInflater.from(this);
        LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.dialog_pay_success, null);
        final AlertDialog dialog = new AlertDialog.Builder(WXPayEntryActivity.this).create();
        dialog.setCanceledOnTouchOutside(false);
        dialog.setOnKeyListener(keyListener);
        dialog.show();
        dialog.getWindow().setContentView(layout);
        ImageView iv_Cancel = (ImageView) layout.findViewById(R.id.iv_Cancel);
        iv_Cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
                Intent intent = new Intent(WXPayEntryActivity.this, MainHomeActivity.class);
                startActivity(intent);
                finish();
            }
        });
        TextView tv_dec = (TextView) layout.findViewById(R.id.tv_dec);
        tv_dec.setText("我们的客服会及时跟您电话沟通，以确认到店护理时间，请留意手机接听！");
        TextView tvContent = (TextView) layout.findViewById(R.id.tvContent);
        tvContent.setVisibility(View.GONE);
        TextView btSure = (TextView) layout.findViewById(R.id.btSure);
        btSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                Intent intent = new Intent(WXPayEntryActivity.this, MainHomeActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }


    private void showFailPopWindow() {
        LayoutInflater inflater = LayoutInflater.from(this);
        LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.dialog_pay_fail, null);
        final AlertDialog dialog = new AlertDialog.Builder(WXPayEntryActivity.this).create();
        dialog.setCanceledOnTouchOutside(false);
        dialog.setOnKeyListener(keyListener);
        dialog.show();
        dialog.getWindow().setContentView(layout);
        TextView tvContent = (TextView) layout.findViewById(R.id.tvContent);
        tvContent.setVisibility(View.VISIBLE);
        ImageView iv_Cancel = (ImageView) layout.findViewById(R.id.iv_Cancel);
        iv_Cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
                Intent intent = new Intent(WXPayEntryActivity.this, MainHomeActivity.class);
                startActivity(intent);
                finish();
            }
        });
        TextView btSure = (TextView) layout.findViewById(R.id.btSure);
        btSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                Intent intent = new Intent(WXPayEntryActivity.this, MainHomeActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
            Intent intent = new Intent(WXPayEntryActivity.this, MainHomeActivity.class);
            startActivity(intent);
        }
        return super.onKeyDown(keyCode, event);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        unsubscribe();
    }

    protected void unsubscribe() {
        if (mDisposable != null && !mDisposable.isDisposed()) {
            mDisposable.dispose();
        }
    }
}